home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / PROGRAMMING / Raven 1.2.sit / Raven 1.2 / Source / Foundation / Common / ZAllocator.h < prev    next >
Text File  |  1997-06-17  |  3KB  |  110 lines

  1. /*
  2.  *  File:       ZAllocator.h
  3.  *  Summary:    Abstract base class for dynamic memory allocators.
  4.  *  Written by: Jesse Jones
  5.  *
  6.  *  Copyright ゥ 1997 Jesse Jones. 
  7.  *    For conditions of distribution and use, see copyright notice in ZTypes.h  
  8.  *
  9.  *  Change History (most recent first):
  10.  *
  11.  *         <->     1/29/97    JDJ        Created
  12.  */
  13.  
  14. #pragma once
  15.  
  16. #include <New.h>
  17.  
  18. #include <ZDebug.h>
  19. #include <ZTypes.h>
  20.  
  21.  
  22. //-----------------------------------
  23. //    Forward References
  24. //
  25. class TFixedAllocator;
  26. class ZSizeDistribution;
  27.  
  28.  
  29. //-----------------------------------
  30. //    Types
  31. //
  32. typedef void (*BlockValidateHook)(const void* block, long size, void* refCon);
  33.  
  34.  
  35. // ===================================================================================
  36. //    class TAllocator
  37. // ===================================================================================
  38. class TAllocator {
  39.  
  40. //-----------------------------------
  41. //    Initialization/Destruction
  42. //
  43. public:
  44.     virtual                ~TAllocator();
  45.  
  46.                         TAllocator();
  47.                         
  48. private:
  49.                         TAllocator(const TAllocator& rhs);
  50.                         
  51.             TAllocator& operator=(const TAllocator& rhs);
  52.                 
  53. //-----------------------------------
  54. //    Allocations
  55. //
  56. public:
  57.     virtual    void*        Allocate(ulong bytes) = 0;
  58.                         // Returns nil if allocation failed.
  59.                         
  60.     virtual    void         Deallocate(void* block) = 0;
  61.     
  62. //-----------------------------------
  63. //    Info
  64. //
  65. public:
  66.     // ----- Heap -----
  67.     virtual ulong         GetHeapSize() const = 0;
  68.             
  69.     virtual ulong         GetPoolCount() const = 0;
  70.                         // Returns the number of pools allocated (after the initial pool).
  71.                         
  72.     // ----- Blocks -----
  73.     virtual    ulong         GetBlockSize(const void* ptr) const = 0;
  74.                         // Note that this may be slightly larger than the size passed to
  75.                         // Allocate.
  76.                         
  77.     virtual    ulong         GetTotalBlockSize(const void* ptr) const = 0;
  78.                         // Includes header and possibly trailer.
  79.  
  80. //-----------------------------------
  81. //    Debugging
  82. //
  83. public:
  84. #if DEBUG
  85.     virtual    void         ValidateBlock(const void* ptr) const = 0;
  86.             
  87.     virtual void         ValidateHeap(BlockValidateHook hook = nil, void* refCon = nil) const = 0;
  88. #endif
  89.  
  90. //-----------------------------------
  91. //    Internal API
  92. //
  93. public:
  94.     static    void*         operator new(size_t size);
  95.                         // Allocators can be used by the global operator new so we'll
  96.                         // take care of allocating ourselves.
  97.  
  98.     static    void         operator delete(void* ptr);
  99.     
  100. #if DEBUG
  101.     static void         TestAllocator(TAllocator& heap1, TAllocator& heap2, TAllocator& heap3);
  102.                         // Runs some timing tests on the specified allocator.
  103.  
  104. protected:
  105.     static void         DoTest(TAllocator& heap, const ZSizeDistribution& distribution);
  106. #endif
  107. };
  108.  
  109.  
  110.